package com.wen.system.shiro.filter;

import com.wen.common.enums.CodeEnum;
import com.wen.common.utils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义form认证过滤器，处理shiro默认认证失败后的一些处理
 * <p>
 *
 * @author devin <devinlive@163.com>
 * @version 1.0
 * @since 2023/07/21 13:22
 */
@Slf4j
@Component
@Order(999)
public class CustomFormAuthenticationFilter extends FormAuthenticationFilter {

    // 修改为认证失败不重定向页面
    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        log.info("身份验证失败，重定向到登录页面");
        HttpServletResponse httpServletResponse = WebUtils.toHttp(response);
        ResponseResult.printResult(httpServletResponse, ResponseResult.error(CodeEnum.USER_NOT_LOGGED_IN));

        return false;
    }

}
